import { IMetaData, IMetaField, IMetaProperty, ITypeStatus } from './type';
import { transfmSchemaValue } from './utils';

export default class MataData {
  /**
   * 唯一标识
   */
  private id: number;

  /**
   * 类型
   */
  private type: ITypeStatus;

  /**
   * 类型的具体表现
   */
  private format?: 'int' | 'double';

  /**
   * 属性
   */
  private properties: IMetaProperty[];

  /**
   * 静态属性
   */
  private fields?: IMetaField[];

  private schema?: IMetaData;

  constructor(props: IMetaData) {
    this.schema = props;
    this.id = props.id;
    this.type = props.type;
    this.format = props.format;
    this.properties = props.properties;
    this.fields = props.fields;
  }

  private static _instance: MataData | null = null;

  /**
   * 获取类的实例
   * @param mataDataOptions
   * @returns
   */
  public static getInstance(mataDataOptions?: IMetaData): MataData {
    if (MataData._instance === null && mataDataOptions) {
      MataData._instance = new MataData(mataDataOptions);
    }
    return MataData._instance!;
  }

  /**
   * 获取元数据的schema
   * @returns
   */
  getMataDataSchema(): IMetaData {
    return this.schema!;
  }

  /**
   * 初始化元数据的实例
   */
  initSchema(): any {
    if (this.schema) {
      // 判断type属于什么类型
      return transfmSchemaValue(this.schema);
    }
    return '暂无元数据schema';
  }
}
